package com.demo.project.jyfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;

import com.demo.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.project.jyfx.mapper.StudentMapper;
import com.demo.project.jyfx.domain.Student;
import com.demo.project.jyfx.service.IStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生信息Service业务层处理
 *
 * @author khks
 * @date 2025-05-29
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService
{
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id)
    {
        return studentMapper.selectById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(student.getGender()), Student :: getGender, student.getGender())
                .like(StringUtils.isNotEmpty(student.getName()), Student :: getName, student.getName());
        return studentMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public String insertStudent(Student student)
    {
        student.setId(studentMapper.getSequenceValues(1).get(0));
        studentMapper.insert(student);
        return "";
    }

    /**
     * 批量新增学生信息
     *
     * @param studentList 学生信息
     * @param operName 操作员
     * @return 结果
     */
    @Override
    @Transactional
    public String insertStudents(List<Student> studentList, String operName)
    {
        List <Long> sequenceValues = studentMapper.getSequenceValues(studentList.size());
        int n = 0;
        for (Student student: studentList){
            student.setId(sequenceValues.get(n));
            n ++;
        }
        saveBatch(studentList);
        return "";
    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public String updateStudent(Student student)
    {
        LambdaUpdateWrapper<Student> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Student :: getAge, student.getAge())
                        .set(Student :: getName, student.getName())
                        .set(Student :: getGender, student.getGender())
                        .eq(Student :: getId, student.getId());
        studentMapper.update(null, lambdaUpdateWrapper);
        return "";
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public String deleteStudentByIds(Long[] ids)
    {
        studentMapper.deleteBatchIds(Arrays.asList(ids));
        return "";
    }
}
